﻿// <copyright file="SqwareaAutofacConfiguration.cs" company="Ecole Normale Supérieure">
// Released under GPLv2
// </copyright>
// <summary>Enable you to easily configure AutoFac for a Web Role with ASP.Net MVC</summary>

namespace Sqwarea.Web.Tools
{
    using System.IO;
    using System.Reflection;
    using Autofac.Builder;
    using Autofac.Configuration;
    using Autofac.Integration.Web;
    using Autofac.Integration.Web.Mvc;
    using Lokad.Cloud;
    using Sqwarea.GameLogic;
    using Sqwarea.Persistence;
    using Sqwarea.Persistence.Azure;
    using Sqwarea.Persistence.Csv;
    using Sqwarea.Persistence.Mock;

    /// <summary>
    /// This class enables you to easily configure AutoFac for a Web Role with ASP.Net MVC
    /// </summary>
    public static class SqwareaAutofacConfiguration
    {
        /// <summary>
        /// Configure AutoFac for a Web Role with ASP.Net MVC
        /// </summary>
        /// <param name="assembly">The assembly of the Web Role (should be Assembly.GetExecutingAssembly())</param>
        /// <param name="currentDir">The current directory of Web Role which contains Lokad.Cloud.config and Sqwarea.AppSettings.config</param>
        /// <param name="containerProvider">The resulting containerProvider which should be referenced by ControllerBuilder.Current.SetControllerFactory(new AutofacControllerFactory(_containerProvider))</param>
        public static void ConfigureWeb(Assembly assembly, string currentDir, out IContainerProvider containerProvider)
        {
            // When the application starts, we need to build the container and register all
            // the dependencies that we want it to manage for us
            var builder = new ContainerBuilder();

            // and then we'll use the AutofacControllerModule to register all the controllers it finds in the given assembly
            builder.RegisterModule(new AutofacControllerModule(assembly));

            // Lokad.Cloud
            builder.RegisterModule(new CloudModule());

            // Loading configuration from the Azure Service Configuration
            // We are on the Cloud if settings.HasValue==true
            var settings = RoleConfigurationSettings.LoadFromRoleEnvironment();
            var onTheCloud = settings.HasValue;

            // Persistence
            if (!onTheCloud && SqwareaConfiguration.UsePersistenceMock)
            {
                builder.RegisterModule(new MockPersistenceModule());
            }
            else
            {
                if (onTheCloud)
                {
                    builder.RegisterModule(new CloudConfigurationModule(settings.Value));
                }
                else
                {
                    builder.RegisterModule(new ConfigurationSettingsReader("autofac", currentDir + "\\Lokad.Cloud.config"));
                }
                
                builder.RegisterModule(new PersistenceModule());
            }

            // GameLogic
            builder.RegisterModule(new GameLogicWebModule());

            // Finally we build the container...
            var container = builder.Build();
            containerProvider = new ContainerProvider(container);

            // And import data if needed
            if (!onTheCloud && SqwareaConfiguration.ImportData)
            {
                var gameProvider = container.Resolve<IGameProvider>();
                var logProvider = container.Resolve<ILogProvider>();

                var assembly2 = Assembly.GetExecutingAssembly();

                gameProvider.ImportKingsFromCsv(new StreamReader(assembly2.GetManifestResourceStream("Sqwarea.Web.Tools.CSV.kings.csv")));
                gameProvider.ImportMapFromCsv(new StreamReader(assembly2.GetManifestResourceStream("Sqwarea.Web.Tools.CSV.map.csv")));
                logProvider.ImportLogsFromCsv(new StreamReader(assembly2.GetManifestResourceStream("Sqwarea.Web.Tools.CSV.logs.csv")));

                if (!SqwareaConfiguration.UsePersistenceMock)
                {
                    // Just an HACK to put new kings Id ; WARNING : do not update GlobalVariables !
                    container.Resolve<IQueueProvider>().PutNewKingsId(1, 10);
                }
            }
        }
    }
}
